package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.KnowledgeService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Knowledge;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Description: 知识页
 * @author: Rock
 * @time: 2020/10/28 11:37
 */
@Controller
@RequestMapping("knowledge")
public class KnowledgeController extends BaseController {

    @Resource
    private KnowledgeService knowledgeService;

    /**
     * @param page
     * @time: 2020/10/27 8:57
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<Knowledge> pages = knowledgeService.list(page);
        mv.addObject("knowledges", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("knowledge/knowledge_list");
        return mv;
    }

    /**
     * 添加
     *
     * @time: 2020/10/22 14:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("knowledge/knowledge_add");
        return mv;
    }

    /**
     * @param name
     * @time: 2020/10/26 22:36
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("checkName")
    public Response<Object> checkName(@RequestParam(required = false) String id, String name) {
        Knowledge knowledge = knowledgeService.getByName(name);
        if (StringUtil.isBlank(id)) {
            if (knowledge != null) {
                return Response.failedResponse("名称已存在");
            }
        } else {
            if (knowledge != null && !id.equals(knowledge.getId())) {
                return Response.failedResponse("名称已存在");
            }
        }
        return Response.successResponse();
    }

    /**
     * @param knowledge
     * @time: 2020/10/26 22:36
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(Knowledge knowledge) {
        ModelAndView mv = new ModelAndView();
        SysUser u = this.getSessionUser();
        knowledge.setCreator(u.getId());
        knowledgeService.add(knowledge, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 编辑页面
     *
     * @param id
     * @time: 2020/10/22 22:16
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("knowledge", knowledgeService.getById(id));
        mv.setViewName("knowledge/knowledge_edit");
        return mv;
    }

    /**
     * @param adversters
     * @time: 2020/10/27 8:52
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(Knowledge knowledge) {
        ModelAndView mv = new ModelAndView();
        knowledgeService.update(knowledge, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 删除
     *
     * @param [id]
     * @time: 2020/9/14 9:41
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public Response<Object> del(String id) {
        return knowledgeService.updateDel(id, this.getSessionUser(), this.getRemortIP());
    }


}
